using System;
using DotNetNuke.Entities.Modules;
using ITCrossing;

/* Testing Pre-conditions
 *  Mock out the DesktopModuleController's .GetDesktopModuleByName() method
 *  Mock the DesktopModuleInfo result to contain the appropriate version as a string
 */

/* Should...
 * RejectUnknownModuleName
 * GetCorrectVersion
 * CompareEqualVersions
 * CompareVersionsForStringValues
 * CompareEqualVersionsForUnequalStringValues - e.g. ver 1.1.0 == ver 01.01.00
 * CompareLessThanVersions
 * CompareGreaterThanVersions
 */
namespace MetaPostProvider.Announcements
{
    /// <summary>
    /// 
    /// </summary>
    /// <remarks>Adapted from http://geekswithblogs.net/narent/archive/2006/05/23/79307.aspx</remarks>
    public class ModuleVersion : IComparable
    {
        string _ModuleName;
        Version _CurrentVersion;

        public Version CurrentVersion
        {get { return _CurrentVersion; } }

        public ModuleVersion(int portalId, string FriendlyName)
        {
            // go through the ModuleDefinitionController and ModuleDefinitionInfo first
            // FriendlyName
            //DotNetNuke.Entities.Modules.Definitions.ModuleDefinitionController con = new DotNetNuke.Entities.Modules.Definitions.ModuleDefinitionController();

            // 1) Get the general ModuleInfo, from which we will use the .DesktopModuleId
            ModuleController cont = new ModuleController();
            ModuleInfo MInf = cont.GetModuleByDefinition(portalId, FriendlyName);
            // 2) Get the DesktopModuleInfo (for the version)
            DesktopModuleController controller = new DesktopModuleController();
            // NOTE: While .GetDesktopModuleByFriendlyName() is obsolete, it becomes "reliable" because the provider's .ProviderKey (when obtained through the MetaPostServices.GetFriendlyNameFromModuleDefinition() method) is "up-to-date"
            //DotNetNuke.Entities.Modules.DesktopModuleInfo info = controller.GetDesktopModuleByFriendlyName(FriendlyName);
            DesktopModuleInfo info = controller.GetDesktopModule(MInf.DesktopModuleID);

            if (info == null)
                throw new MetaPostException("UnknownModuleName", "ModuleVersion cannot find the requested module (ModuleName value is unknown).");
            _CurrentVersion = new Version(info.Version);
            _ModuleName = FriendlyName;
        }

        /// <summary>
        /// Compares the current ModuleVersion object to a specified Version
        /// and returns an indication of their relative values.
        /// </summary>
        /// <param name="obj"></param>
        /// <returns><value>Less than zero</value></returns>
        public int CompareTo(object obj)
        {
            return CurrentVersion.CompareTo(obj);
        }

        /// <summary>
        /// Compares the current ModuleVersion object to a specified Version
        /// (as a string) and returns an indication of their relative values.
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public int CompareTo(string obj)
        {
            return CurrentVersion.CompareTo(new Version(obj));
        }

        /// <summary>
        /// Returns true if this ModuleVersion is before (older than)
        /// the supplied otherVersion
        /// </summary>
        /// <param name="otherVersion"></param>
        /// <returns></returns>
        public Boolean IsVersionOlderThan(string otherVersion)
        {
            return CompareTo(otherVersion) == -1;
        }

        /// <summary>
        /// Returns true if this ModuleVersion is after (newer than)
        /// the supplied otherVersion
        /// </summary>
        /// <param name="otherVersion"></param>
        /// <returns></returns>
        public Boolean IsVersionEqualOrNewerThan(string otherVersion)
        {
            return CompareTo(otherVersion) >= 0;
        }

        /// <summary>
        /// Returns true if this ModuleVersion is idential to
        /// the supplied otherVersion
        /// </summary>
        /// <param name="otherVersion"></param>
        /// <returns></returns>
        public Boolean IsVersionEqualTo(string otherVersion)
        {
            return CompareTo(otherVersion) == 0;
        }
    }
}

/* Notes on the Constructor
 *      There are basically two ways to get the DesktopModuleInfo for a module, but the "recommended" way is to go through the .GetDesktopModule() method of the DesktopModuleController. But, that method requires that you know the DesktopModuleID.
 */